package com.librarySystem.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.librarySystem.dao.BookDaoInterface;
import com.librarySystem.dto.PageDTO;
import com.librarySystem.dto.QueryRestrictionsDTO;
import com.librarySystem.entity.Book;

@Service
public class BookService implements BookServiceInterface {
	@Resource
	private BookDaoInterface bookDao;

	@Override
	@Transactional
	public List<Book> find(QueryRestrictionsDTO queryRestrictionsDTO,PageDTO pageDTO) {
		// TODO Auto-generated method stub
		List<Book> result = bookDao.find(queryRestrictionsDTO,pageDTO);
		if(result != null){
			return result;
		}else{
			return null;
		}
	}
	
	
	
	@Override
	@Transactional
	public Book update(Book book){
		Book result = bookDao.update(book);
		if(result != null){
			if(result.getCount() > 0 || book.getCount() > 0){
				result.setCount(result.getCount()+ book.getCount());
			}else{
				return result;
			}
		}
		saveBook(result);
		return result;
	}
	
	public void saveBook(Book book){
		bookDao.saveBook(book);
	}



	@Override
	@Transactional
	public int findCount(QueryRestrictionsDTO queryRestrictionsDTO) {
		
		bookDao.findCount(queryRestrictionsDTO);
		
		return bookDao.findCount(queryRestrictionsDTO);
	}

}
